﻿/*
*
* 功 能： 系统登录界面后台
* 类 名： Default
*
* Ver    变更日期  20131125   负责人  黄永生  变更内容
* ───────────────────────────────────

*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Model;
using DQYT.HTDZ.Web;

namespace Best.Framework.Web
{
    public partial class Default : PageBase
    {
        private string UserName;
        private string UserPwd;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                try
                {
                    lblMsg.Text = "";
                    UserName = txtUsername.Value.Trim();
                    UserPwd = txtPwd.Value.Trim();

                    if (UserName != "" && UserPwd != "")
                    {
                    }
                    else
                    {
                        lblMsg.Text = "请输入全部信息！";
                        return;
                    }

                    BDUserBLL dUser = new BDUserBLL();
                    int iOk = dUser.CheckUserAndPwd(UserName, UserPwd);
                    switch (iOk)
                    {
                        case 0:
                            lblMsg.Text = "密码错误！";
                            break;
                        case -1:
                            lblMsg.Text = "用户名不存在！";
                            break;
                        case 1:
                            BDUserModel mUser = new BDUserModel();
                            mUser = dUser.GetModel(UserName);
                            
                            Globals.CurrentUserInfo = mUser;
                            SaveAuthTicket(mUser.USE_NAME, false);
                            HttpCookie user = new HttpCookie("user");//定义Cookie
                            user["name"] = Server.UrlEncode(mUser.USE_NAME);
                            Response.Cookies.Add(user);
                            string url = "index.htm";
                            
                            Response.Redirect(url);
                            break;
                    }
                }
                catch (Exception ex)
                {
                    lblMsg.Text = "错误信息：" + ex.Message;
                }

                if (!string.IsNullOrEmpty(Request.QueryString["quit"]) && Request.QueryString["quit"] == "1")
                {
                    Session.RemoveAll();
                    Session.Abandon();
                    FormsAuthentication.SignOut();
                }
            }
        }
        /// <summary>
        /// 登录按钮触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                lblMsg.Text = "";
                UserName = txtUsername.Value.ToString();
                UserPwd = txtPwd.Value.ToString();

                if (UserName != "" && UserPwd != "")
                {
                }
                else
                {
                    lblMsg.Text = "请输入全部信息！";
                    return;
                }

                BDUserBLL dUser = new BDUserBLL();
                int iOk = dUser.CheckUserAndPwd(UserName, UserPwd);
                switch (iOk)
                {
                    case 0:
                        lblMsg.Text = "密码错误！";
                        break;
                    case -1:
                        lblMsg.Text = "用户名不存在！";
                        break;
                    case 1:
                        BDUserModel mUser = new BDUserModel();
                        mUser = dUser.GetModel(UserName);
                        Session["UserName"] = UserName;
                        string username = Session["UserName"].ToString();
                        Globals.CurrentUserInfo = mUser;
                        SaveAuthTicket(mUser.USE_NAME, false);
                        HttpCookie user = new HttpCookie("user");//定义Cookie
                        user["name"] = Server.UrlEncode(mUser.USE_NAME);
                        Response.Cookies.Add(user);
                        string url = "index.htm";
                        Response.Redirect(url);
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息：" + ex.Message;
            }

        }
        /// <summary>
        /// 生成用户验证ticket
        /// </summary>
        /// <param name="accountID"></param>
        /// <param name="isPersistent"></param>
        private void SaveAuthTicket(string accountName, bool isPersistent)
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
               1, // Ticket version
               accountName, // Username associated with ticket
               DateTime.Now, // Date/time issued
               DateTime.Now.AddMonths(6), // Date/time to expire
               isPersistent, // "true" for a persistent user cookie
               accountName, // User-data, in this case the roles
               FormsAuthentication.FormsCookiePath);// Path cookie valid for

            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(
               FormsAuthentication.FormsCookieName, // Name of auth cookie
               hash); // Hashed ticket

            if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }
}
